/*
   Copyright (c) 2011, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-20 Marco Riccardi - initial implementation
 *  2011-09-10 Marco Riccardi - changed .idx file
 *  2015-01-11 Marco Riccardi - added .msx e .wsx files
 *  
 */
package org.mizartools.example.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
//import java.util.regex.Matcher;



import org.mizartools.system.ElementFactory;
import org.mizartools.system.ElementParseException;
import org.mizartools.system.EnvironFile;
import org.mizartools.system.IElement;
import org.mizartools.system.WorkFileExtension;
import org.mizartools.utility.Html;
import org.mizartools.utility.UtilityFile;
import org.mizartools.utility.xml.XMLElement;
import org.mizartools.utility.xml.XMLFile;

public class FileWork {

	private static XMLFile xmlFile = new XMLFile();
	private static ElementFactory elementFactory = ElementFactory.newInstance();

	public static String check(File file, String extension) throws ElementParseException {
		synchronized (FileWork.class) {
			StringBuffer sb = new StringBuffer("");
			LinkedList<XMLElement> xmlElement1List;
			xmlFile.setFile(file);
			xmlElement1List = xmlFile.getXMLElementList();
			//sb.append(" [" + xmlElement1List.size() + "]");
			try {
				String text = UtilityFile.readFile(file);
				String textParsed = createText(xmlElement1List, extension);
				if (extension.equals("msx") || extension.equals("wsx")){
					text = Html.changeCharsHex(text);
					textParsed = Html.changeCharsHex(textParsed);
				}
				if (!text.equals(textParsed)) {
		    		sb.append("\n");
					sb.append("Error during test of parsing of " + file.getName());
		    		sb.append("\n");
		    		sb.append("ORIGINAL START\n");
		    		sb.append(text + "\n");
		    		sb.append("ORIGINAL END\n");
					
		    		sb.append("\n");
		    		sb.append("PARSED START\n");
		    		sb.append(textParsed + "\n");
		    		sb.append("PARSED END\n");
		    		String arrayText[] = text.split("\n");
		    		String arrayTextParsed[] = textParsed.split("\n");
		    		for (int i = 0; i < arrayText.length; i++){
		    			if (!arrayText[i].equals(arrayTextParsed[i])){
				    		sb.append("\n");
				    		sb.append("LINE ["+i+"]\n");
				    		sb.append("\n");
				    		sb.append("ORIGINAL LINE\n");
				    		sb.append(arrayText[i] + "\n");
				    		sb.append("ORIGINAL END\n");
				    		sb.append("\n");
				    		sb.append("PARSED LINE\n");
				    		sb.append(arrayTextParsed[i] + "\n");
				    		sb.append("PARSED END\n");
		    				break;
		    			}
		    		}
		    		
				}
			} catch (FileNotFoundException e) {
				throw new ElementParseException(ElementParseException.FILE_NOT_FOUND, "Error opening the file " + file.toString());
			} catch (IOException e) {
				throw new ElementParseException(ElementParseException.IO, "Error reading the file " + file.toString());
			}
			if (!sb.toString().equals("")) return sb.toString(); 
			if (extension.equals("msx")) return sb.toString();
			if (extension.equals("wsx")) return sb.toString();
			
			IElement element = elementFactory.createElement(file, WorkFileExtension.getClassName(extension));
			LinkedList<XMLElement> xmlElement2List = element.getXMLElementList();
			clearCharacters(xmlElement1List);
			clearCharacters(xmlElement2List);
			if (!xmlElement1List.equals(xmlElement2List)){
				while (!xmlElement1List.isEmpty()){
					XMLElement xmlElement1 = xmlElement1List.poll();
					XMLElement xmlElement2 = xmlElement2List.poll();
					if (!xmlElement1.equals(xmlElement2)){
			    		sb.append("\n");
						sb.append("Error during test of parsing of " + file.getName());
			    		sb.append("\n");
						
			    		sb.append("\n");
			    		sb.append("ORIGINAL START\n");
			    		sb.append(xmlElement1.toString() + "\n");
			    		sb.append("ORIGINAL END\n");
						
			    		sb.append("\n");
			    		sb.append("PARSED START\n");
			    		sb.append(xmlElement2.toString() + "\n");
			    		sb.append("PARSED END\n");
						
			    		sb.append("\n");
			    		sb.append("ORIGINAL TAIL START\n");
						for (XMLElement xmlElement3: xmlElement1List){
				    		sb.append(xmlElement3.toString() + "\n");
						}
			    		sb.append("ORIGINAL TAIL END\n");
					}
				}
	    		sb.append("\n");
				sb.append("Error during test of parsing of " + file.toString());
	    		sb.append("\n");
			}
			return sb.toString();
		}
	}

	private static void clearCharacters(LinkedList<XMLElement> xmlElementList){
		for (XMLElement xmlElement : xmlElementList){
			xmlElement.setCharacters("\n");
		}
	}
	
	private static String createText(LinkedList<XMLElement> xmlElementList, String extension){
		String ret = "\n";
		String mizFiles = EnvironFile.getMIZFILES() + File.separator;
		StringBuffer sb = new StringBuffer("");
		sb.append("<?xml version=\"1.0\"?>");
		sb.append(ret);
		if (!extension.equals("dcx") && 
				!extension.equals("evl") &&
				!extension.equals("frm") &&
				!extension.equals("frx") &&
				!extension.equals("idx") &&
				!extension.equals("parx") &&
				!extension.equals("refx") &&
				!extension.equals("vcl") &&
				!extension.equals("msx") &&
				!extension.equals("wsx")) { 
			sb.append("<?xml-stylesheet type=\"text/xml\" href=\"file://" + mizFiles + "miz.xml\"?>");
			sb.append(ret);
		}
		for (XMLElement e: xmlElementList){
			if (e.getName().equals("Symbol")) {
				sb.append(e.toXMLString());
			} else {
				sb.append(e.toString());
			}
		}
		sb.append(ret);
		return sb.toString();
	}
	
}
